<?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
<?php
    $meta = get_post_meta( get_the_ID() ); 
    
    // Get total score
    $score = (float)$meta['odesk-job-score'][0];
    $start_w = ($score/5.0) * 80; // 100% = 80px
    
    // Try to get score list
    $scores =  unserialize((string)$meta['odesk-job-scores'][0]);
    
    
    // Retrive the tags list.
    $posttags = get_the_tags($post->ID);
    // print_r($posttags);
    // print_r(get_the_date());
    
	$pattern = get_shortcode_regex();
	$gallery = array();
    if (   preg_match_all( '/'. $pattern .'/s', $post->post_content, $matches )
        && array_key_exists( 2, $matches )
        && in_array( 'gallery', $matches[2] ) )   	{

		if(preg_match_all('/[0-9]+/', $matches[0][0], $id_arr, PREG_PATTERN_ORDER) !== false) {
			// print_r($id_arr);
			/* $_attachments = get_posts( array('include' => implode(',', $id_arr[0]) , 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image' ) ); */
			foreach($id_arr[0] as $id) {
				$thumb['thumbnail'] = wp_get_attachment_image_src( $id, 'thumbnail');
				$thumb['large'] = wp_get_attachment_image_src( $id, 'large');
				// print_r ( $thumb );
				$gallery[] = $thumb;
			}
		}
    }
?>
<div class="portfolio-detail">
    <a href="#" class="back-btn button" onclick="showList();">&larr;</a>
    <!-- <h1 class="large-title"><?php echo " " . get_the_title(); ?></h1> -->
    <div class="left-col box-shadow">
        <div class="content ">
            <?php
	            $thumb = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'large');
                $thumb = get_template_directory_uri() . "/timpthumb.php?src=" . urlencode( $thumb[0] ) . "&h=260&w=610q=90";
            ?>
			<img src="<?php echo $thumb; ?>" alt="">
            <div class="description">
                <div class="info-box">
                    (<?php echo get_the_date('M - Y'); ?>)
                    <strong><?php echo " " . get_the_title(); ?></strong><br>
                    <?php the_excerpt(); ?>
                    <div class="nowrap">
                        <div class="oStarsContainer oStarsMedium" title="<?php echo $score; ?> stars"> 
                            <div class="oStarsValue" style="width:<?php echo $start_w; ?>px"></div> 
                        </div>
                    </div>
                    
                </div>
            </div>
            <div class="feedback">
                <div class="comment">
                    <?php the_content(); ?>
                </div>
                <div class="scores">
                    <ul>
                        <table width=100%>
                        <?php 
                            $item_score;
                            $item_score_w;
                            foreach($scores->score as $item) {
                                $item_score = (float) $item->score;
                                $item_score_w = ($item_score/5.0) * 45; //Mini star 100% ~ 45px;
                                $item_score_w = (string) $item_score_w . 'px';
                                echo "<tr>";
                                echo "<td><label for=\"\">$item->label</label></td>";
                                echo "<td><div class=\"oStarsContainer oStarsSmall\" title=\"$item_score stars\"> <div class=\"oStarsValue\" style=\"width:$item_score_w\"></div> </div></td>";
                                echo "</tr>";
                            }
                        ?>
                        </table>
                    </ul>
                </div>
                
            </div>
        </div>
    </div>
    <div class="right-col">
        <div class="box">
            <a href="#" class="button profile-btn" onclick="alert('We are updating ...'); return false;">Visit live site</a> <br>
            <a target=_blank href="https://www.odesk.com/users/~017b947e6bb09dadc5" class="button profile-btn">Checkout my oDesk Profile</a>
        </div>
        <?php if($posttags) {                ?>
            <div class="box">
                <h2>Features</h2>
                <ul id="features">
                    <?php foreach($posttags as $tag) { ?>
                        <li><em></em><a href="#" onclick="return false;"><?php echo $tag->name; ?></a></li>
                    <?php } ?>
                </ul>
            </div>
        <?php } ?>
		<?php
		if(isset($gallery) && count($gallery) > 0) :
				
			
		?>
        <div class="box">
            <h2>Gallery</h2>
            <ul id="gallery">
                <?php foreach($gallery as $image)  { ?>
				<li>
                    <a href="<?php echo $image['large'][0]; ?>" class="grouped_elements" rel="gallery">
						<img class="box-shadow" src="<?php echo get_template_directory_uri() . "/timpthumb.php?src=" . urlencode( $image['thumbnail'][0] ) . "&h=50&w=70q=100"; ?>" alt="">
					</a>
                </li>
				<?php } ?>
            </ul>
        </div>
		<?php endif; ?>
    </div>
</div>
<?php endwhile; // end of the loop. ?> 
<script>
	$(function() {
		// Init fancybox
		$("a.grouped_elements").fancybox();
	})
</script>